home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / misc.c < prev    next >
C/C++ Source or Header  |  1992-06-18  |  2KB  |  121 lines

  1. /* Miscellaneous machine independent utilities */
  2.  
  3.  
  4. #include <ctype.h>
  5. #include "global.h"
  6.  
  7. #ifdef    UNIX
  8. #undef    toupper
  9. #undef    tolower
  10. #endif
  11.  
  12. /* Convert hex-ascii to integer */
  13. int
  14. htoi(s)
  15. char *s;
  16. {
  17.     int i = 0;
  18.     char c;
  19.  
  20.     while((c = *s++) != '\0'){
  21.         if(c == 'x')
  22.             continue;    /* allow 0x notation */
  23.         if('0' <= c && c <= '9')
  24.             i = (i * 16) + (c - '0');
  25.         else if('a' <= c && c <= 'f')
  26.             i = (i * 16) + (c - 'a' + 10);
  27.         else if('A' <= c && c <= 'F')
  28.             i = (i * 16) + (c - 'A' + 10);
  29.         else
  30.             break;
  31.     }
  32.     return i;
  33. }
  34. /* replace terminating end of line marker(s) with null */
  35. rip(s)
  36. register char *s;
  37. {
  38.     register char *cp;
  39.  
  40.     if((cp = index(s,'\r')) != NULLCHAR)
  41.         *cp = '\0';
  42.     if((cp = index(s,'\n')) != NULLCHAR)
  43.         *cp = '\0';
  44. }
  45. /* Case-insensitive string comparison */
  46. strncasecmp(a,b,n)
  47. register char *a,*b;
  48. register int n;
  49. {
  50.     char a1,b1;
  51.  
  52.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  53.         if(a1 == b1)
  54.             continue;    /* No need to convert */
  55.         a1 = tolower(a1);
  56.         b1 = tolower(b1);
  57.         if(a1 == b1)
  58.             continue;    /* NOW they match! */
  59.         if(a1 > b1)
  60.             return 1;
  61.         if(a1 < b1)
  62.             return -1;
  63.     }
  64.     return 0;
  65. }
  66.  
  67. int16
  68. get16(cp)
  69. register char *cp;
  70. {
  71.     register int16 x;
  72.  
  73.     x = uchar(*cp++);
  74.     x <<= 8;
  75.     x |= uchar(*cp);
  76.     return x;
  77. }
  78.  
  79. /* Machine-independent, alignment insensitive network-to-host long conversion */
  80. int32
  81. get32(cp)
  82. register char *cp;
  83. {
  84.     int32 rval;
  85.  
  86.     rval = uchar(*cp++);
  87.     rval <<= 8;
  88.     rval |= uchar(*cp++);
  89.     rval <<= 8;
  90.     rval |= uchar(*cp++);
  91.     rval <<= 8;
  92.     rval |= uchar(*cp);
  93.  
  94.     return rval;
  95. }
  96.  
  97. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  98.  * library, but it doesn't call mallocw() and can therefore return NULL.
  99.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  100.  */
  101. char *
  102. strdup(s)
  103. const char *s;
  104. {
  105.     register char *out;
  106.     register int len;
  107.  
  108.     if(s == NULLCHAR)
  109.         return NULLCHAR;
  110.     len = strlen(s);
  111.     out = malloc(len+1);
  112.     if (!out) {
  113.         printf("strdup: malloc failed\n");
  114.         exit(1);
  115.     }
  116.     /* This is probably a tad faster than strcpy, since we know the len */
  117.     memcpy(out,s,len);
  118.     out[len] = '\0';
  119.     return out;
  120. }
  121.